package com.zj.ssm.service.impl;

import com.zj.ssm.mapper.CustMapper;
import com.zj.ssm.mapper.CustomerMapper;
import com.zj.ssm.pojo.domain.UserLogin;
import com.zj.ssm.pojo.domain.Cust;
import com.zj.ssm.pojo.domain.CustLogin;
import com.zj.ssm.pojo.domain.vo.UsernameVo;
import com.zj.ssm.result.AjaxResult;
import com.zj.ssm.service.CustomerService;
import com.zj.ssm.sysconst.LoginConst;
import com.zj.ssm.util.SendCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * @author lilinhui
 * @author diao 2022/1/7 星期五 20:27
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private HttpSession session;

    @Autowired
    private CustMapper custMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private com.zj.ssm.mapper.LoginMapper loginMapper;

    @Override
    public AjaxResult login(String username, String password) {

        CustLogin custLogin = customerMapper.isCustLogin(username, password);

        if (custLogin == null){
            return AjaxResult.fail("对不起账号密码错误");
        }

        if (custLogin.getState() == 1){
            return AjaxResult.fail("对不起  账号冻结");
        }

        Integer uid = custLogin.getUid();

        Cust cust = custMapper.getCustByID(uid);

        session.setAttribute("cust",cust);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult sendCode(String telephone) {

        String sendcode = SendCodeUtil.sendcode(telephone);
        session.setAttribute(telephone,sendcode);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult register(UsernameVo usernameVo) {
        System.out.println(usernameVo+"+++++++++++++++++");
        Object attribute = session.getAttribute(usernameVo.getTelephone());
        if (attribute == null){
            return AjaxResult.fail("短信无效");
        }
        if (!usernameVo.getCode().equals(attribute)){
            return AjaxResult.fail("对不起验证码不正确");
        }

        UserLogin test1 = loginMapper.test1User(usernameVo.getUsername());
        if (test1 != null){
            return AjaxResult.fail(LoginConst.USER_MSG_ZHANGHAOBEIZHUCE);
        }

//        int register =  LoginMapper.register(usernameVo.getUsername(),usernameVo.getPassword(),usernameVo.getTelephone());

        int row = loginMapper.register1(usernameVo);


        if (row>0){
            AjaxResult success = AjaxResult.success();
            System.out.println(success);
            return AjaxResult.success();
        }

        return AjaxResult.fail("注册失败");
    }

    @Override
    public AjaxResult udpatePwd(String password, String newPwd) {
        UserLogin userLogin = loginMapper.test2User(password);

        if (userLogin == null){
            return AjaxResult.fail(LoginConst.USER_MSG_MIMABUPIPEI);
        }
        int i = loginMapper.updatePwd(newPwd);

        if (i>0){
            return AjaxResult.success();
        }


        return AjaxResult.fail();
    }

    @Override
    public AjaxResult updatePassword(String username, String password) {

        UserLogin u = loginMapper.test1User(username);

        if (u == null){
            return AjaxResult.fail(LoginConst.USER_MSG_ZHANGHAOBUCUNZAI);
        }

        int i = loginMapper.updatePassword(username, password);
        if (i>0){
            return AjaxResult.success();
        }

        return AjaxResult.fail();
    }


}
